package com.binhu;

import com.binhu.entity.BinhuObj;
import com.yidong.entity.YiDongObj;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;

import java.io.*;
import java.text.DecimalFormat;
import java.util.Iterator;

public class Shoutuodaixiao {

    public static String PsbgPathIn = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\binhucfg\\受托代销\\";
    public static String PsbgPathOut = "D:\\code\\Projects\\yidong\\src\\main\\java\\com\\binhucfg\\";

//    @FunctionalInterface
//    public interface WordReadCallback {
//        void processText(String text);
//    }

    //#region 评审报告word 123
    public void rangeFile(BinhuObj obj){
        String[] files = {"1评审报告（询价）.docx","2排除围标串标报告.docx","3评审准备（纪律）.docx"};
        for (String fileName : files) {
            this.psbgWord(obj,fileName);
        }
        psbgExcel(obj);
    }

    //转化word
    public void psbgWord(BinhuObj obj,String fileName){
        String inPath = PsbgPathIn + fileName;
        String outPath = PsbgPathOut + fileName;

        try (InputStream is = new FileInputStream(inPath);
             XWPFDocument document = new XWPFDocument(is)) {
            // 遍历文档中的所有段落
            for (XWPFParagraph paragraph : document.getParagraphs()) {
                for (XWPFRun run : paragraph.getRuns()) {
                    String runText = run.text();

                    runText = this.PsbgOneText(runText,obj);
                    run.setText(runText, 0);
                }
            }

            // 遍历文档中的所有表格
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        for (XWPFParagraph cellParagraph : cell.getParagraphs()) {
                            for (XWPFRun cellRun : cellParagraph.getRuns()) {
                                String runText = cellRun.text();
//                                System.out.println(runText);

                                runText = this.PsbgOneText(runText,obj);
                                cellRun.setText(runText, 0);
                            }
                        }
                    }
                }
            }

            // 保存文档到新文件
            try (OutputStream out = new FileOutputStream(outPath)) {
                document.write(out);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //#endregion


    //#region 综合评审表excel
    public void psbgExcel(BinhuObj obj){
        String fileName = "4评审综合用表(合并).xlsx";
        String inPath = PsbgPathIn + fileName;
        String outPath = PsbgPathOut + fileName;

        try (FileInputStream fis = new FileInputStream(inPath);
             Workbook workbook = new XSSFWorkbook(fis)) {
            Iterator<Sheet> sheetIterator =  workbook.sheetIterator();
            while(sheetIterator.hasNext()){
                Sheet sheet = sheetIterator.next();
                for (Row row : sheet) {
                    for (Cell cell : row) {
                        // 只在文本类型的单元格中查找和替换
                        if (cell.getCellType() == CellType.STRING) {
                            String originalText = cell.getStringCellValue();
                            String runText = originalText;

                            //替换
                            runText = this.PsbgOneText(runText,obj);
                            // 如果替换后的文本与原始文本不同，则更新单元格
                            if (!originalText.equals(runText)) {
                                cell.setCellValue(runText);
                            }
                        }
                    }
                }
            }

//            Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet，索引从0开始

            // 将修改后的Workbook写入到新文件
            try (FileOutputStream fos = new FileOutputStream(outPath)) {
                workbook.write(fos);
                System.out.println("Excel文件已成功保存为: " + outPath);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //#endregion

    public String PsbgOneText(String runText, BinhuObj obj){
        //todo 替换
        runText = runText.replaceAll("%项目名称%",obj.getXmmc());
        runText = runText.replaceAll("%项目编号%",obj.getXmbh());
        runText = runText.replaceAll("%采购单位%",obj.getCgdw());
        runText = runText.replaceAll("%日期%",obj.getRqnyr());
        runText = runText.replaceAll("%挂网日期%",obj.getGwrq());
        runText = runText.replaceAll("%开标日期%",obj.getKbrq());
        runText = runText.replaceAll("%最高限价%",obj.getZgxj());

        runText = runText.replaceAll("%设备费%",obj.getSbf());
        runText = runText.replaceAll("%设备费不含税%",buhanshui(obj.getSbf(),0.13d));

        runText = runText.replaceAll("%手续费一%", obj.getSxf1());
        runText = runText.replaceAll("%手续费一不含税%",buhanshui(obj.getSxf1(),0.06d));

        runText = runText.replaceAll("%手续费二%", obj.getSxf2());
        runText = runText.replaceAll("%手续费二不含税%",buhanshui(obj.getSxf2(),0.06d));

        runText = runText.replaceAll("%手续费三%", obj.getSxf3());
        runText = runText.replaceAll("%手续费三不含税%",buhanshui(obj.getSxf3(),0.06d));

        runText = runText.replaceAll("%供应商一%",obj.getGys1());
        runText = runText.replaceAll("%供应商二%",obj.getGys2());
        runText = runText.replaceAll("%供应商三%",obj.getGys3());

        runText = runText.replaceAll("%采购人代表%",obj.getCgrdb());

        return runText;
    }

    //不含税 = 含税 / （1 + 税率）
    public String buhanshui(String hanshuiStr,double i){
        double hanshuiNum = Double.parseDouble(hanshuiStr);
        double buhanshuiNum = hanshuiNum / ( 1 + i );
        DecimalFormat df = new DecimalFormat("#.00"); // 创建一个格式化对象，保留两位小数
        String result = df.format(buhanshuiNum);
        return result;
    }


}
